import { Injectable } from "@nestjs/common";
import { InjectRepository } from "@nestjs/typeorm";

import { BlogEntity } from './blog.entity'
import { TagService } from './../tag/tag.service'
import { Repository } from "typeorm";

@Injectable()
export class BlogService {
    constructor(
        @InjectRepository(BlogEntity)
        private readonly blogService: Repository<BlogEntity>,
        private tagsService: TagService,
    ) { }
    /**
     * 后端分页
     * @param blog 
     */
    // async findAllBlog(blog: { page: number, pagesize: number }) {
    //     let page=blog.page?blog.page:1,
    //         pagesize=blog.pagesize?blog.pagesize:10;
    //     return await this.blogService
    //         .createQueryBuilder('blog')
    //         .leftJoinAndSelect('blog.user', 'user')
    //         .leftJoinAndSelect('blog.tags', 'tag')
    //         .take(pagesize)
    //         .skip(pagesize * (page-1))
    //         .getMany();
    // }
    async findAllBlog() {
        return await this.blogService
            .createQueryBuilder('blog')
            .leftJoinAndSelect('blog.user', 'user')
            .leftJoinAndSelect('blog.tags', 'tag')
            .getMany();
    }
    async add(blog) {
        let tagsList = await this.tagsService.findByIds(blog.tags)
        blog.tags = tagsList;
        return await this.blogService.save(blog)
    }
    async remove(blogId) {
        return await this.blogService.delete(blogId)
    }

}